﻿//***************************************************
//
//       File Name:    StartItem.cs
//
//       Author:   王文军    Email:  546527816@qq.com
//
//
//***************************************************

using XHFrameWork;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// 启动类
/// </summary>
public class StartItem : MonoBehaviour {

    private float waitTiemr = 60f;
    private float timers;
    public bool isTimer = true;
    public bool isSend = false;
    private bool isClickA = false;
    private bool isClickB = false;
    private bool isEsc = false;
    
    private AudioManager audioManager;
    private bool showEff = false;
    private GameObject effcet;
    RaycastHit hit;
    Ray ray;
    private float mouse2;

    void Awake()
    {
    }

    void Start()
    {
        //QualitySettings.SetQualityLevel(5);
        //Screen.SetResolution(1920, 1080, true);
        //Screen.fullScreen = true;
        effcet = NGUITools.AddChild(GameObject.FindGameObjectWithTag("UICamera"), ResManager.Instance.Load("Prefabs/Effects/baoguang") as GameObject);
        effcet.transform.localPosition = Vector3.zero;
        effcet.transform.localScale = Vector3.one;//
        effcet.SetActive(false);
        waitTiemr= float.Parse(ReadXmlManager.OnReadXml("PeiZhi.xml", "PeiZhiInfo", "无人操作时反回待机界面等待时间", "Timer"));
        audioManager = FindObjectOfType<AudioManager>();
        timers = waitTiemr;
        ModuleManager.Instance.RegisterAllModules();
        UIManager.Instance.OpenUICloseOthers(EnumUIType.E_StartInterface);
    }

    void Update()
    {
        if (Input.touchCount <= 0 && Input.GetAxis("Mouse Y")==0&& Input.GetAxis("Mouse X")==0&&!isTimer)
        {  
            waitTiemr -= Time.deltaTime;
            if(waitTiemr<=0)
            {
                UIManager.Instance.OpenUICloseOthers(EnumUIType.E_StartInterface);
                waitTiemr = timers;
                isTimer = true;
                isSend = false;
            }
        }
        else {  if (waitTiemr < timers) waitTiemr = timers; }
        if (Input.GetKeyDown(KeyCode.Escape))
            Application.Quit();
        if (Input.GetKeyDown(KeyCode.S))
            UIManager.Instance.OpenUI(EnumUIType.E_Background);


        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
        {
            NewRay(Input.GetTouch(0).position);
        }
        if (Input.GetMouseButtonDown(0))
        {
            NewRay(Input.mousePosition);
        }
    }

    //void OnGUI()
    //{
    //    GUILayout.Label(waitTiemr.ToString());
    //}
    private void NewRay(Vector3 vt)
    {
        if (showEff == true) return;
        ray = GameObject.FindGameObjectWithTag("UICamera").GetComponent<Camera>().ScreenPointToRay(vt);
        if (Physics.Raycast(ray, out hit))
        {
            if (hit.collider.tag == "Button")
            {
                NewEffcet(hit.collider.gameObject.transform.position);
            }
        }
    }
    /// <summary>
    /// 显示按钮特效
    /// </summary>
    /// <param name="vt"></param>
    private void NewEffcet(Vector3 vt)
    {
        effcet.SetActive(true);
        effcet.transform.position = vt;
        audioManager.PlayButton();
        StartCoroutine(ShowEffcetTime());
    }

    IEnumerator ShowEffcetTime()
    {
        yield return new WaitForSeconds(0.2f);
        effcet.SetActive(false);
        showEff = false;
    }

}
